/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package di15groep1;

import di15groep1.AutoVerbruik.AutoVerbruikBerekening;
import di15groep1.AutoVerbruik.AutoVerbruiksGegevens;
import di15groep1.afstandsbereking.AfstandBerekening;
import di15groep1.adresbepaling.AdresBepaling;
import javax.swing.JOptionPane;

/**
 *
 * @author mm026191
 */
public class MainForm extends javax.swing.JFrame {
String brandstof = null;
    /**
     * Creates new form MainForm
     */
    public MainForm() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jTKenteken = new javax.swing.JTextField();
        jTPostCodeFrom = new javax.swing.JTextField();
        jTPostCodeTo = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jBtgetKenteken = new javax.swing.JButton();
        jBtgetAdresFrom = new javax.swing.JButton();
        jBtgetAdresTo = new javax.swing.JButton();
        jTextKentekenInfo = new javax.swing.JTextField();
        jTAdresFrom = new javax.swing.JTextField();
        jTAdresTo = new javax.swing.JTextField();
        jBbereken = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setText("Kenteken");

        jLabel2.setText("StartAdres");

        jLabel3.setText("EindAdres");

        jBtgetKenteken.setText("Get Kenteken");
        jBtgetKenteken.setActionCommand("");
        jBtgetKenteken.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtgetKentekenActionPerformed(evt);
            }
        });

        jBtgetAdresFrom.setText("Get Adres From");
        jBtgetAdresFrom.setActionCommand("");
        jBtgetAdresFrom.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtgetAdresFromActionPerformed(evt);
            }
        });

        jBtgetAdresTo.setText("Get Adres To");
        jBtgetAdresTo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtgetAdresToActionPerformed(evt);
            }
        });

        jBbereken.setText("Berekenen");
        jBbereken.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBberekenActionPerformed(evt);
            }
        });

        jTextArea1.setColumns(20);
        jTextArea1.setRows(5);
        jScrollPane1.setViewportView(jTextArea1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(22, 22, 22)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jBbereken)
                                .addGap(0, 0, Short.MAX_VALUE))
                            .addComponent(jScrollPane1)))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(jLabel2)
                            .addComponent(jLabel3))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jTKenteken)
                            .addComponent(jTPostCodeFrom)
                            .addComponent(jTPostCodeTo, javax.swing.GroupLayout.PREFERRED_SIZE, 92, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jBtgetAdresFrom, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jBtgetKenteken, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jBtgetAdresTo, javax.swing.GroupLayout.PREFERRED_SIZE, 107, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jTextKentekenInfo, javax.swing.GroupLayout.DEFAULT_SIZE, 277, Short.MAX_VALUE)
                            .addComponent(jTAdresFrom)
                            .addComponent(jTAdresTo))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(59, 59, 59)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jTKenteken, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel1)
                            .addComponent(jBtgetKenteken)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(58, 58, 58)
                        .addComponent(jTextKentekenInfo, javax.swing.GroupLayout.PREFERRED_SIZE, 77, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTPostCodeFrom, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2)
                    .addComponent(jBtgetAdresFrom)
                    .addComponent(jTAdresFrom, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTPostCodeTo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3)
                    .addComponent(jBtgetAdresTo)
                    .addComponent(jTAdresTo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(20, 20, 20)
                .addComponent(jBbereken)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 108, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

   
    private void jBberekenActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBberekenActionPerformed

        String from = getSubStringPostcode(jTPostCodeFrom.getText());
        String to = getSubStringPostcode(jTPostCodeTo.getText());
        String afstand = AfstandBerekening.getAfstand(from, to);
        String verbruik = AutoVerbruiksGegevens.getVerbruiksGegevens(this.jTKenteken.getText());
        vulBerekening(afstand , verbruik);
        System.out.println(afstand + verbruik);
    }//GEN-LAST:event_jBberekenActionPerformed
    private String getSubStringPostcode(String postCode) {
        return postCode.substring(0, 6);
    }
    
    private void vulBerekening(String afstand, String verbruik){
        if (jTAdresFrom.getText().length() ==0){
                jBtgetAdresFromActionPerformed(null);
        }
        if (this.jTAdresTo.getText().length() == 0){
            jBtgetAdresToActionPerformed(null);
        }
        if (jTextKentekenInfo.getText().length() == 0){
            jBtgetKentekenActionPerformed(null);
        }
        
        String from = this.jTAdresFrom.getText();// adresGegevens(jTPostCodeFrom.getText());
        String to = this.jTAdresTo.getText();
        String auto = this.jTextKentekenInfo.getText();
         double a = Double.parseDouble(afstand);
         double v = Double.parseDouble(verbruik);
      String kosten =   AutoVerbruikBerekening.berekening(brandstof, a, v);
        
        this.jTextArea1.setText("de afstand tussen: \n"
                + "" + from + "\n en \n" + to + "\n is: \n" + afstand + "km \n"
                + "De kosten voor de " + auto + " zijn: "  + kosten + " Euro." );
    }
    
    private void jBtgetKentekenActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBtgetKentekenActionPerformed
        if (this.jTKenteken.getText().length() > 0) {
            di15groep1.AutoInformatie.CarInfo carInfo = getCarInfo(this.jTKenteken.getText());
            if (carInfo != null) {
                this.brandstof = carInfo.getBrandstofType();
                this.jTextKentekenInfo.setText(carInfo.getMerk() + " " + carInfo.getModel());
            }
        }

    }//GEN-LAST:event_jBtgetKentekenActionPerformed
    private void jBtgetAdresFromActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBtgetAdresFromActionPerformed
 
        this.jTAdresFrom.setText(adresGegevens(this.jTPostCodeFrom.getText()));
    }//GEN-LAST:event_jBtgetAdresFromActionPerformed

    private void jBtgetAdresToActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBtgetAdresToActionPerformed
        this.jTAdresTo.setText(adresGegevens(this.jTPostCodeTo.getText()));



    }//GEN-LAST:event_jBtgetAdresToActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(MainForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(MainForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(MainForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(MainForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new MainForm().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jBbereken;
    private javax.swing.JButton jBtgetAdresFrom;
    private javax.swing.JButton jBtgetAdresTo;
    private javax.swing.JButton jBtgetKenteken;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextField jTAdresFrom;
    private javax.swing.JTextField jTAdresTo;
    private javax.swing.JTextField jTKenteken;
    private javax.swing.JTextField jTPostCodeFrom;
    private javax.swing.JTextField jTPostCodeTo;
    private javax.swing.JTextArea jTextArea1;
    private javax.swing.JTextField jTextKentekenInfo;
    // End of variables declaration//GEN-END:variables

    private static di15groep1.AutoInformatie.CarInfo getCarInfo(java.lang.String kenteken) {
        di15groep1.AutoInformatie.Webservice service = new di15groep1.AutoInformatie.Webservice();
        di15groep1.AutoInformatie.WebserviceSoap port = service.getWebserviceSoap();
        return port.getCarInfo(kenteken);
    }

    private String adresGegevens(String postCode) {

        if (postCode.length() > 6) {
            String adres = AdresBepaling.giveAddres(postCode);
            return adres;
        } else {
            JOptionPane.showMessageDialog(this, "postcode minimaal 7 karakters", "Postcode Fout!", JOptionPane.ERROR_MESSAGE);
            return "";
        }
    }
}
